
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/



#include "miniutil/ipaccess.h"

using namespace miniutil::net;

void ip_tables_rule::add(  miniutil::ip4addr & addr, int res )
{
	this->tables[addr.addrData() ] = res;
}

void ip_tables_rule::clear()
{
	this->tables.clear();
}

int  ip_tables_rule::check(  miniutil::ip4addr & addr )
{
	miniutil::m_uint32_t ipaddr = addr.addrData();
	if ( this->tables.find( ipaddr ) != tables.end() )
		return tables[ ipaddr ];

	return this->default_res;
}


void ip_tables_rule::config( miniutil::tinyxml::TiXmlElement * ele ) 
{
	ele->QueryIntAttribute("default_res", & default_res );
	ele->QueryIntAttribute("matched_res", & matched_res );

	miniutil::tinyxml::TiXmlElement * e_ip;
	for( e_ip = ele->FirstChildElement("ip");
		e_ip != NULL;
		e_ip = e_ip->NextSiblingElement("ip")
		)
	{
		std::string addrstr;
		e_ip->QueryValueAttribute("addr", &addrstr );
		if ( addrstr.length() == 0 )
			continue;
		miniutil::ip4addr addr( addrstr );
		int res;
		if ( e_ip->QueryIntAttribute("res" , &res ) != miniutil::tinyxml::TIXML_SUCCESS )
			res = this->matched_res;

		if ( res != this->default_res )
			this->tables[addr.addrData()] = res;
	}
}


void cnet_tables_rule::add(  miniutil::ip4addr &addr, int res )
{
	this->tables[addr.addrData() & 0x00ffffff ] = res;	
}

int cnet_tables_rule::check(  miniutil::ip4addr & addr )
{
	miniutil::m_uint32_t ipaddr = addr.addrData() & 0x00ffffff;

	if ( this->tables.find( ipaddr ) != tables.end() )
		return tables[ ipaddr ];

	return this->default_res;
}

void cnet_tables_rule::config( miniutil::tinyxml::TiXmlElement * ele ) 
{
	ele->QueryIntAttribute("default_res", & default_res );
	ele->QueryIntAttribute("matched_res", & matched_res );

	miniutil::tinyxml::TiXmlElement * e_ip;
	for( e_ip = ele->FirstChildElement("cnet");
		e_ip != NULL;
		e_ip = e_ip->NextSiblingElement("cnet")
		)
	{
		std::string addrstr;
		e_ip->QueryValueAttribute("addr", &addrstr );
		if ( addrstr.length() == 0 )
			continue;
		miniutil::ip4addr addr( addrstr );
		int res;
		if ( e_ip->QueryIntAttribute("res" , &res ) != miniutil::tinyxml::TIXML_SUCCESS )
			res = this->matched_res;

		if ( res != this->default_res )
		{
			miniutil::m_uint32_t ipaddr = addr.addrData() & 0x00ffffff ;
			this->tables[ ipaddr ] = res;
		}
	}
}



void ip_cnet_rule::clear()
{
	this->ip_table.clear();
	this->cnet_table.clear();
}

int  ip_cnet_rule::check(  miniutil::ip4addr & addr )
{
	int res  = this->ip_table.check( addr );
	if ( res != this->ip_table.default_res )
		return res;

	return this->cnet_table.check( addr );
}

void ip_cnet_rule::config( miniutil::tinyxml::TiXmlElement * ele ) 
{
	miniutil::tinyxml::TiXmlElement * ele_iptable = ele->FirstChildElement("ip_tables");
	if ( ele_iptable )
		this->ip_table.config( ele_iptable);

	miniutil::tinyxml::TiXmlElement * ele_cnettable = ele->FirstChildElement("cnet_tables");
	if ( ele_cnettable )
		this->cnet_table.config( ele_cnettable);

}



ip_concurrent_rule::ip_concurrent_rule( int max_con ) : i_max_con_per_ip( max_con )
{
}
int  ip_concurrent_rule::check(  miniutil::ip4addr & addr )
{
	miniutil::m_uint32_t ipaddr = addr.addrData();

	if ( this->tables.find( ipaddr ) == tables.end() )
		return 0;

	return tables[ipaddr ];
}

void ip_concurrent_rule::connected( miniutil::ip4addr & addr )
{
	miniutil::m_uint32_t ipaddr = addr.addrData();
    int count = tables[ipaddr];
    tables[ipaddr] = count + 1;

	//if ( this->tables.find( ipaddr ) == tables.end() )
	//{
	//	tables[ipaddr] = 1;
	//	return ;
	//}
	//tables[ipaddr] += 1;
}

void ip_concurrent_rule::disconnected( miniutil::ip4addr & addr )
{
	miniutil::m_uint32_t ipaddr = addr.addrData();
    int count = tables[ipaddr];
    if ( count <= 1 )
        tables.erase( ipaddr );
    else
        tables[ipaddr] = count - 1;

	//if ( this->tables.find(ipaddr ) == tables.end() )
	//{
	//	return ;
	//}
	//if ( tables[ipaddr] <= 1 )
	//{
	//	tables.erase( ipaddr );
	//	return;
	//}
	//tables[ipaddr] -= 1;
}

void ip_concurrent_rule::config( miniutil::tinyxml::TiXmlElement * ele ) 
{
	ele->QueryIntAttribute("maxcon", & i_max_con_per_ip );
}
